// pages/date/date.js
const util = require('../../utils/util')
// 初始化云数据库环境
let db = wx.cloud.database({
  env: 'junsheng0108-9gsv99le6b9d0ec9'
});
// 连接数据表
let job = db.collection('db_job');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '',
    show: false,
    isSort: false,
    isShow: false,
    listDate: [],
    colorArr: [
      "#fcb126", "#567cf4", "#40b1fc", "#7FFFAA", "#ff7070",
      "#EEC900", "#4876FF", "#ff6100", "#FFA500",
      "#7DC67D", "#E17572", "#7898AA", "#C35CFF", "#33BCBA", "#C28F5C",
      "#FF8533", "#6E6E6E", "#428BCA", "#5cb85c", "#FF674F", "#E9967A",
      "#66CDAA", "#00CED1", "#9F79EE", "#CD3333", "#FFC125", "#32CD32",
      "#00BFFF", "#68A2D5", "#FF69B4", "#DB7093", "#CD3278", "#607B8B"
    ],
  },


  async getJobList(job_date) {
    await job.where({
        job_date
      }).get()
      .then(content => {
        console.log(content)
        let {
          data,
          errMsg
        } = content;
        if (errMsg == "collection.get:ok") {
          this.setData({
            listDate: data,
          })
          let newlistDate = this.sortTimeTop(this.data.listDate, "job_time")
          this.setData({
            listDate: newlistDate,
          })
          if (data.length == 0) {
            this.setData({
              isShow: false
            })
          } else {
            this.setData({
              isShow: true
            })
          }
        }
      })
      .catch(error => {
        console.log(error);
      })
  },
  // 排序
  sortTimeTop(array, key) {
    return array.sort((b, a) => {
      let x = a[key].slice(0, 2);
      let y = b[key].slice(0, 2);
      return y - x
    })
  },
  sortTimeUnder(array, key) {
    return array.sort((b, a) => {
      let x = a[key].slice(0, 2);
      let y = b[key].slice(0, 2);
      return x - y
    })
  },
  // 改变排序状态值
  changeSort() {
    this.setData({
      isSort: !this.data.isSort
    })
    if (!this.data.isSort) {
      let top = this.sortTimeTop(this.data.listDate, "job_time")
      this.setData({
        listDate: top,
      })
    } else {
      let under = this.sortTimeUnder(this.data.listDate, "job_time")
      this.setData({
        listDate: under,
      })
    }
  },


  onDisplay() {
    this.setData({
      show: true
    });
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
  },
  onConfirm(event) {
    this.setData({
      show: false,
      date: this.formatDate(event.detail),
    });
    this.getJobList(this.data.date)
    console.log('111', this.data.listDate);
console.log('22',this.formatDate(event.detail));
  },




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})